home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Development / Source / Telnet 2.6.1d1 4⁄26⁄94 Folder / source / ftp / rcp.c < prev   
Text File  |  1993-04-09  |  9KB  |  398 lines

  1. #if 0
  2.  
  3. #define ga()  while (!netwrite(rsnum,"",1)) netsleep(0)
  4.  
  5. /************************************************************************/
  6. /*  unsetrshd
  7. *   remove the acceptance of rshd calls (rcp)
  8. */
  9. unsetrshd()
  10.     {
  11.     netclose(rsnum);
  12.     rsnum = -1;
  13.     rcpenable = 0;
  14. }
  15.  
  16. /************************************************************************/
  17.  
  18. setrshd()
  19. {
  20.     int i;
  21. /*
  22. *  set up to receive a rsh call connection 
  23. */
  24.     if (rsnum >= 0)
  25.         return(0);
  26.     curstate = 199;                    /* waiting for connection */
  27.     i = netsegsize(RCPSEGSIZE);
  28.     rsnum = netlisten(HRSHD,0);        /* BYU 2.4.15 */
  29.     netsegsize(i);
  30.     if (rsnum >= 0)
  31.         Sptypes[rsnum] = PRCP;
  32.  
  33.     rcpenable = 1;
  34. }
  35.  
  36.  
  37. /************************************************************************/
  38. /*  rshell
  39. *   take an incoming rshell request and service it.  Designed to handle
  40. *   rcp primarily.
  41. */
  42. rshd(code)
  43.     int code;
  44.     {
  45.     int i,j;
  46.  
  47.     if (!rcpenable)
  48.         return(0);
  49.  
  50.     switch (curstate) {
  51.         case 199:                    /* wait to get started */
  52.             if (code != CONOPEN)
  53.                 break;
  54.  
  55.             curstate = 0;
  56.             netputuev(SCLASS,RCPACT,rsnum);        /* keep us alive */
  57.  
  58.             break;
  59.             
  60. /*
  61. * in effect, this is a subroutine that captures network traffic while
  62. * waiting for a specific character to be received
  63. */
  64.         case 50:
  65.             while (0 < (cnt = netread(rsnum,&xs[waitpos],1))) {
  66.                 if (xs[waitpos] == waitchar) {
  67.                     curstate = retstate;
  68.                     netputuev(SCLASS,RCPACT,rsnum);        /* keep us alive */
  69.                     break;
  70.                 }
  71.                 else 
  72.                     waitpos += cnt;
  73.             }
  74.             netpush(rsnum);
  75.             break;
  76.  
  77.         case 51:                /* for recursion, passes straight through */
  78.             break;
  79.  
  80.         case 0:                    /* waiting for first string */
  81.             retstate = 1;
  82.             curstate = 50;
  83.             waitchar = 0;
  84.             waitpos = 0;
  85.             netputuev(SCLASS,RCPACT,rsnum);        /* keep us alive */
  86.             break;
  87.  
  88.         case 1:                    /* we have received stderr port number */
  89.             i = atoi(xs);        /* port number */
  90.             curstate = 51;
  91. #ifdef notneeded
  92. /*
  93. *  caution, netrespond calls netsleep()
  94. *  which will call this routine
  95. *  careful with the synchronicity!
  96. */
  97.             if (i)        /* zero means, don't bother */
  98.                 rserr = netrespond(i,rsnum,1);    /* respond to rsh */
  99.             else
  100. #else
  101.             if (i) {
  102.                 cnt = -1;        /* abort it all, we don't take rsh */
  103.                 break;
  104.             }
  105.             else
  106. #endif
  107.                 rserr = -1;
  108.  
  109.             retstate = 2; curstate = 50;
  110.             waitpos = 0; waitchar = 0;
  111.             break;
  112.  
  113.         case 2:                /* get user name, my machine */
  114.             strncpy(myuser,xs,16);
  115.  
  116.             retstate = 3; curstate = 50;
  117.             waitpos = 0; waitchar = 0;
  118.             break;
  119.  
  120.         case 3:             /* get user name, his machine */
  121.             strncpy(hisuser,xs,16);
  122. /*            ftransinfo(hisuser); */
  123.  
  124.             retstate = 4; curstate = 50;
  125.             waitchar = 0; waitpos = 0;
  126.  
  127.             break;
  128.  
  129.         case 4:
  130. /*            ftransinfo(xs);*/
  131. /*
  132. * ACK receipt of command line
  133. */
  134.             if (rserr >= 0)
  135.                 netwrite(rserr,&xp,1);        /* send null byte */
  136.             else {
  137.                 ga();            /* send NULL on main connection */
  138.             }
  139.  
  140.             if (!strncmp(xs,"rcp ",4)) {
  141. /*
  142. *  rcp will be using wildcards, target must be a directory
  143. */
  144.                 if (!strncmp(&xs[4],"-d -t",5)) {
  145.                     strncpy(pathname,&xs[10],PATHLEN);
  146.                     if (direxist(pathname)) {
  147. /*                        ftransinfo("no directory by that name ");*/
  148.                         netwrite(rsnum,"\001 No dir found ",16);
  149.                         netpush(rsnum);
  150.                         cnt = -1;
  151.                         break;
  152.                     }
  153.  
  154.                     isdir = 1;
  155.                     retstate = 20; curstate = 50;
  156.                     waitchar = '\012'; waitpos = 0;
  157.  
  158.                     ga();        /* ready for them to start */
  159.                     break;
  160.                 }
  161. /*
  162. * target could be a directory or a complete file spec
  163. */
  164.                 if (!strncmp(&xs[4],"-t",2)) {
  165.                     strncpy(pathname,&xs[7],PATHLEN);
  166.                     if (!direxist(pathname)) 
  167.                         isdir = 1;
  168.                     else
  169.                         isdir = 0;
  170.  
  171.                     retstate = 20 ; curstate = 50;
  172.                     waitchar = '\012'; waitpos = 0;
  173.  
  174.                     ga();            /* ready for rcp to start */
  175.                     break;
  176.                 }
  177. /*
  178. *  rcp is requesting me to transfer file(s) (or giving directory name)
  179. */
  180.                 if (!strncmp(&xs[4],"-f",2)) {
  181.                     strncpy(pathname,&xs[7],PATHLEN);
  182.  
  183. /*
  184. *  direxist returns whether the path spec refers to a directory, and if
  185. *  it does, prepares it as a prefix.  Therefore, if it is a dir, we append
  186. *  a '*' to it to wildcard all members of the directory.
  187. *  Firstname() takes a file spec (with wildcards) and returns a pointer
  188. *  to a prepared ACTUAL file name.  nextname() returns successive ACTUAL
  189. *  filenames based on firstname().
  190. */
  191.                     if (!direxist(pathname)) {
  192.                         i = strlen(pathname);
  193.                         pathname[i] = '*';        /* all members of directory*/
  194.                         pathname[++i] = '\0';
  195.                     }
  196.                     nextfile = firstname(pathname);
  197.  
  198.                     if (nextfile == NULL) {
  199. /*                        ftransinfo(" file or directory not found ");*/
  200.                         netwrite(rsnum,"\001 File not found ",18);
  201.                         netpush(rsnum);
  202.                         cnt = -1;
  203.                     }
  204.                     else {
  205.                         /* wait for other side to be ready */
  206.                         retstate = 30;    curstate = 50;
  207.                         waitchar = 0; waitpos = 0;
  208.                     }
  209.                     break;
  210.                 }
  211.             }
  212.  
  213.             break;
  214.  
  215.         case 20:
  216.             xs[waitpos] = '\0';        /* add terminator */
  217.  
  218. /*
  219. *  get working values from command line just received
  220. *  open file for receive
  221. */
  222.             if (xs[0] != 'C' || xs[5] != ' ') {
  223. /*                ftransinfo(" Cannot parse filename line "); */
  224.                 netwrite(rsnum,"\001 Problem with file name ",26);
  225.                 cnt = -1;
  226.                 break;
  227.             }
  228.  
  229.             filelen = atol(&xs[6]);
  230.  
  231.             for (i = 6; xs[i] != ' '; i++) 
  232.                 if (!xs[i]) {
  233. /*                    ftransinfo(" premature EOL ");*/
  234.                     netwrite(rsnum,"\001 Problem with file name ",26);
  235.                     cnt = -1;
  236.                     break;
  237.                 }
  238.  
  239.             strcpy(newfile,pathname);        /* path spec for file */
  240.  
  241.             if (isdir)                        /* add file name for wildcards */
  242.                 strcat(newfile,&xs[++i]);
  243.  
  244.             if (0 > (fh = creat(newfile,O_RAW))) {
  245.                 netwrite(rsnum,"\001 Cannot open file for write ",29);
  246.                 cnt = -1;
  247.                 break;
  248.             }
  249.             netputevent(USERCLASS,RCPBEGIN,-1);
  250.             ga();                            /* start sending the file to me */
  251.             xp = len = 0;
  252.             curstate = 21;                    /* receive file, fall through */
  253.             break;
  254.  
  255.         case 21:
  256.             do {
  257.             /* wait until xs is full before writing to disk */
  258.                 if (len <= 0) {
  259.                     if (xp) {
  260.                         write(fh,xs,xp);
  261.                         xp = 0;
  262.                     }
  263.                     if (filelen > (long)BUFFERS)
  264.                         len = BUFFERS;
  265.                     else
  266.                         len = (int)filelen;
  267.                 }
  268.  
  269.                 cnt = netread(rsnum,&xs[xp],len);
  270.  
  271.                 filelen -= (long)cnt;
  272.                 len -= cnt;
  273.                 xp += cnt;
  274.  
  275. /*                printf(" %ld %d %d %d ",filelen,len,xp,cnt);
  276.                 n_row(); putln(""); */
  277.  
  278.                 if (filelen <= 0L || cnt < 0) {
  279.                     write(fh,xs,xp);        /* write last block */
  280.                     close(fh);
  281.                     fh = 0;
  282.                     
  283.                     /* wait for NULL byte at end after closing file */
  284.                     curstate = 50;  retstate = 22;
  285.                     waitchar = 0;   waitpos = 0;
  286.                     break;
  287.                 }
  288.  
  289.             } while (cnt > 0);
  290.             break;
  291.  
  292.         case 22:
  293.             /* cause next sequence of bytes to be saved as next filename
  294.                 to transfer     */
  295.             ga();            /* tell other side, I am ready */
  296.             waitchar = '\012'; waitpos = 0;
  297.             curstate = 50; retstate = 20;
  298.             break;
  299.  
  300. /*
  301. *  transfer file(s) to the sun via rcp
  302. */
  303.         case 30:
  304.             if (0 > (fh = open(nextfile,O_RAW))) {
  305.                 netwrite(rsnum,"\001 File not found ",19);
  306. /*                ftransinfo("Cannot open file to transfer: ");
  307.                 ftransinfo(nextfile); */
  308.                 cnt = -1;
  309.                 break;
  310.             }
  311.             netputevent(USERCLASS,RCPBEGIN,-1);
  312.             filelen = lseek(fh,0L,(short)2);    /* how long is file? */
  313.             lseek(fh,0L,0);                /* back to beginning */
  314.  
  315.             for (i=0,j=-1; nextfile[i] ; i++)
  316.                 if (nextfile[i] == '\\')
  317.                     j = i;
  318.  
  319.             sprintf(xs,"C0755 %lu %s\012",filelen,&nextfile[j+1]);
  320.             netwrite(rsnum,xs,strlen(xs));    /* send info to other side */
  321.  
  322. /*            ftransinfo(xs);                     check it */
  323.  
  324.             retstate = 31; curstate = 50;
  325.             waitchar = 0;  waitpos = 0;
  326.  
  327.             towrite = xp = 0;
  328.             break;
  329.  
  330.         case 31:
  331. /*
  332. *   we are in the process of sending the file 
  333. */
  334.             netputuev(SCLASS,RCPACT,rsnum);        /* keep us alive */
  335.  
  336.             if (towrite <= xp) {
  337.                 towrite = read(fh,xs,BUFFERS);
  338.                 xp = 0;
  339.                 filelen -= (long)towrite;
  340.             }
  341.             i = netwrite(rsnum,&xs[xp],towrite-xp);
  342.             if (i > 0)
  343.                 xp += i;
  344.  
  345. /*            printf(" %d %d %d %ld\012",i,xp,towrite,filelen);
  346.             n_row();
  347. */
  348. /*
  349. *  done if:  the file is all read from disk and all sent
  350. *  or other side has ruined connection
  351. */
  352.             if ((filelen <= 0L && xp >= towrite) || netest(rsnum)) {
  353.                 close(fh);
  354.                 fh = 0;
  355.                 nextfile = nextname();        /* case of wildcards */
  356.                 ga(); 
  357.                 netputuev(SCLASS,RCPACT,rsnum);
  358.                 if (nextfile == NULL)
  359.                     retstate = 32;
  360.                 else
  361.                     retstate = 30;
  362.                 curstate = 50;
  363.                 waitchar = 0;    waitpos = 0;
  364.             }
  365.             break;
  366.         case 32:
  367.             cnt = -1;
  368.             break;
  369.         case 5:
  370.             break;
  371.         default:
  372.             break;
  373.  
  374.     }
  375.  
  376. /*
  377. *  after reading from connection, if the connection is closed,
  378. *  reset up shop.
  379. */
  380.     if (cnt < 0) {
  381.         if (fh > 0) {
  382.             close(fh);
  383.             fh = 0;
  384.         }
  385.         curstate = 5;
  386.         cnt = 0;
  387.         netclose(rsnum);
  388.         rsnum = -1;
  389.         netputevent(USERCLASS,RCPEND,-1);
  390.  
  391.         setrshd();                    /* reset for next transfer */
  392.     }
  393.  
  394.  
  395. }
  396.  
  397. #endif
  398.